<?php

namespace app\controllers;

use app\models\IcdCodes;

use lithium\security\Auth;
use lithium\action\DispatchException;

class IcdCodesController extends \lithium\action\Controller {

	public function index() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$icdCodes = IcdCodes::all(array(
			'order' => array('name' => 'ASC')
		));
		return compact('icdCodes');
	}

	public function view() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$icdCode = IcdCodes::first($this->request->id);
		return compact('icdCode');
	}

	public function add() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$icdCode = IcdCodes::create();

		if (($this->request->data) && $icdCode->save($this->request->data)) {
			return $this->redirect(array('IcdCodes::view', 'args' => array($icdCode->id)));
		}
		return compact('icdCode');
	}

	public function edit() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$icdCode = IcdCodes::find($this->request->id);

		if (!$icdCode) {
			return $this->redirect('IcdCodes::index');
		}
		if (($this->request->data) && $icdCode->save($this->request->data)) {
			return $this->redirect(array('IcdCodes::view', 'args' => array($icdCode->id)));
		}
		return compact('icdCode');
	}

	public function delete() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		if (!$this->request->is('post') && !$this->request->is('delete')) {
			$msg = "IcdCodes::delete can only be called with http:post or http:delete.";
			throw new DispatchException($msg);
		}
		IcdCodes::find($this->request->id)->delete();
		return $this->redirect('IcdCodes::index');
	}
}

?>